<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>承运商主页 - 航运物流系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .header {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .logo h1 {
            color: #333;
            font-size: 24px;
        }
        
        .logo p {
            color: #666;
            font-size: 14px;
        }
        
        .user-info {
            text-align: right;
        }
        
        .user-info p {
            margin-bottom: 5px;
            color: #555;
        }
        
        .logout-btn {
            background: #e74c3c;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.3s ease;
        }
        
        .logout-btn:hover {
            background: #c0392b;
        }
        
        .content {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
        }
        
        .panel {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        
        .panel-title {
            font-size: 20px;
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #667eea;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }
        
        .form-group input, .form-group textarea {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e1e1;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        
        .form-group input:focus, .form-group textarea:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .form-group textarea {
            min-height: 100px;
            resize: vertical;
        }
        
        .btn {
            padding: 12px 25px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        
        .shipments-list {
            max-height: 400px;
            overflow-y: auto;
        }
        
        .shipment-item {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 15px;
            border-left: 4px solid #667eea;
            transition: all 0.3s ease;
        }
        
        .shipment-item:hover {
            background: #e9ecef;
            transform: translateX(5px);
        }
        
        .shipment-id {
            font-weight: bold;
            color: #333;
            margin-bottom: 5px;
        }
        
        .shipment-detail {
            color: #666;
            font-size: 14px;
            margin-bottom: 3px;
        }
        
        .status-badge {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: bold;
        }
        
        .status-pending {
            background: #fff3cd;
            color: #856404;
        }
        
        .status-shipped {
            background: #d1ecf1;
            color: #0c5460;
        }
        
        .status-delivered {
            background: #d4edda;
            color: #155724;
        }
        
        .update-form {
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid #eee;
        }
        
        .ship-icon {
            font-size: 24px;
            color: #667eea;
            margin-right: 10px;
        }
        
        .empty-message {
            text-align: center;
            color: #666;
            padding: 30px;
        }
        
        .error-message {
            color: #e74c3c;
            margin-top: 15px;
            font-size: 14px;
            display: none;
        }
        
        .success-message {
            color: #27ae60;
            margin-top: 15px;
            font-size: 14px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <div class="logo">
                <h1>航运物流管理系统</h1>
                <p>承运商操作平台</p>
            </div>
            <div class="user-info">
                <p>承运商ID: <span id="carrierIdDisplay">-</span></p>
                <button class="logout-btn" onclick="logout()">退出登录</button>
            </div>
        </div>
        
        <div class="content">
            <div class="panel">
                <h2 class="panel-title"><span class="ship-icon">📦</span>物流信息列表</h2>
                <div class="shipments-list" id="shipmentsList">
                    <div class="empty-message">暂无物流信息</div>
                </div>
            </div>
            
            <div class="panel">
                <h2 class="panel-title"><span class="ship-icon">🔄</span>更新物流状态</h2>
                <form id="updateForm">
                    <div class="form-group">
                        <label for="shipmentId">物流单号</label>
                        <input type="number" id="shipmentId" name="shipmentId" required placeholder="请输入物流单号">
                    </div>
                    
                    <div class="form-group">
                        <label for="status">新状态</label>
                        <select id="status" name="status" class="form-group" required>
                            <option value="">请选择状态</option>
                            <option value="pending">待发货</option>
                            <option value="shipped">运输中</option>
                            <option value="delivered">已送达</option>
                        </select>
                    </div>
                    
                    <button type="submit" class="btn">更新状态</button>
                    
                    <div class="error-message" id="updateError"></div>
                    <div class="success-message" id="updateSuccess"></div>
                </form>
                
                <div class="update-form">
                    <h2 class="panel-title"><span class="ship-icon">➕</span>新增承运商</h2>
                    <form id="registerForm">
                        <div class="form-group">
                            <label for="name">承运商名称</label>
                            <input type="text" id="name" name="name" required placeholder="请输入承运商名称">
                        </div>
                        
                        <div class="form-group">
                            <label for="info">承运商信息</label>
                            <textarea id="info" name="info" required placeholder="请输入承运商详细信息"></textarea>
                        </div>
                        
                        <div class="form-group">
                            <label for="regPassword">密码</label>
                            <input type="password" id="regPassword" name="regPassword" required placeholder="请输入密码">
                        </div>
                        
                        <button type="submit" class="btn">注册承运商</button>
                        
                        <div class="error-message" id="registerError"></div>
                        <div class="success-message" id="registerSuccess"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 获取承运商ID
        const carrierId = localStorage.getItem('carrierId');
        if (!carrierId) {
            window.location.href = 'carrier_login.html';
        } else {
            document.getElementById('carrierIdDisplay').textContent = carrierId;
            loadShipments();
        }
        
        // 加载物流信息列表
        function loadShipments() {
            fetch(`http://172.21.136.66:8080/carrier/shipments?id=${encodeURIComponent(carrierId)}`)
            .then(response => response.json())
            .then(data => {
                if (data.code === '0') {
                    displayShipments(data.data);
                } else {
                    document.getElementById('shipmentsList').innerHTML = '<div class="empty-message">加载失败：' + (data.message || '未知错误') + '</div>';
                }
            })
            .catch(error => {
                document.getElementById('shipmentsList').innerHTML = '<div class="empty-message">网络错误，请稍后重试</div>';
            });
        }
        
        // 显示物流信息列表
        function displayShipments(shipments) {
            const shipmentsList = document.getElementById('shipmentsList');
            
            if (!shipments || shipments.length === 0) {
                shipmentsList.innerHTML = '<div class="empty-message">暂无物流信息</div>';
                return;
            }
            
            let html = '';
            shipments.forEach(shipment => {
                // 假设返回的数据结构是 {id, from, to, status}
                html += `
                    <div class="shipment-item">
                        <div class="shipment-id">物流单号: ${shipment.id}</div>
                        <div class="shipment-detail">起始地: ${shipment.from}</div>
                        <div class="shipment-detail">目的地: ${shipment.to}</div>
                        <div class="shipment-detail">状态: <span class="status-badge status-${shipment.status}">${getStatusText(shipment.status)}</span></div>
                    </div>
                `;
            });
            
            shipmentsList.innerHTML = html;
        }
        
        // 获取状态文本
        function getStatusText(status) {
            switch(status) {
                case 'pending': return '待发货';
                case 'shipped': return '运输中';
                case 'delivered': return '已送达';
                default: return status;
            }
        }
        
        // 更新物流状态表单提交
        document.getElementById('updateForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const shipmentId = document.getElementById('shipmentId').value;
            const status = document.getElementById('status').value;
            const updateError = document.getElementById('updateError');
            const updateSuccess = document.getElementById('updateSuccess');
            
            // 发送更新请求到后端
            fetch('http://172.21.136.66:8080/shipment/update', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ id: shipmentId, status: status })
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === '0') {
                    updateSuccess.textContent = '物流状态更新成功！';
                    updateSuccess.style.display = 'block';
                    updateError.style.display = 'none';
                    
                    // 清空表单
                    document.getElementById('shipmentId').value = '';
                    document.getElementById('status').value = '';
                    
                    // 重新加载物流信息列表
                    setTimeout(() => {
                        loadShipments();
                        updateSuccess.style.display = 'none';
                    }, 2000);
                } else {
                    updateError.textContent = '更新失败：' + (data.message || '未知错误');
                    updateError.style.display = 'block';
                    updateSuccess.style.display = 'none';
                }
            })
            .catch(error => {
                updateError.textContent = '网络错误，请稍后重试';
                updateError.style.display = 'block';
                updateSuccess.style.display = 'none';
            });
        });
        
        // 注册承运商表单提交
        document.getElementById('registerForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const name = document.getElementById('name').value;
            const info = document.getElementById('info').value;
            const password = document.getElementById('regPassword').value;
            const registerError = document.getElementById('registerError');
            const registerSuccess = document.getElementById('registerSuccess');
            
            // 发送注册请求到后端
            fetch('http://172.21.136.66:8080/carrier/add', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `name=${encodeURIComponent(name)}&info=${encodeURIComponent(info)}&password=${encodeURIComponent(password)}`
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === '0') {
                    registerSuccess.textContent = '承运商注册成功！';
                    registerSuccess.style.display = 'block';
                    registerError.style.display = 'none';
                    
                    // 清空表单
                    document.getElementById('name').value = '';
                    document.getElementById('info').value = '';
                    document.getElementById('regPassword').value = '';
                    
                    setTimeout(() => {
                        registerSuccess.style.display = 'none';
                    }, 3000);
                } else {
                    registerError.textContent = '注册失败：' + (data.message || '未知错误');
                    registerError.style.display = 'block';
                    registerSuccess.style.display = 'none';
                }
            })
            .catch(error => {
                registerError.textContent = '网络错误，请稍后重试';
                registerError.style.display = 'block';
                registerSuccess.style.display = 'none';
            });
        });
        
        // 退出登录
        function logout() {
            localStorage.removeItem('carrierId');
            window.location.href = 'carrier_login.html';
        }
    </script>
</body>
</html>